﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SQLite;

namespace EmailStorm.Data
{
    public class Task
    {

        public DataSet GetTaskList()
        {
            string sql = "select * from task";
            SqliteAccess obj = new SqliteAccess();
  
            return obj.ExecuteDataSet(sql, null);
        }
        public IDataReader GetTaskById(int taskId) {
            string sql = "select * from task where task_id=@task_id";
            SqliteAccess obj = new SqliteAccess();
            SQLiteParameter[] parameters = new SQLiteParameter[]{
                new SQLiteParameter("task_id",taskId)
            };
            
            return (IDataReader)obj.ExecuteReader(sql, parameters);

        }

        public int GetTaskCount() {
            string sql = "select count(*) from task ";
            SqliteAccess obj = new SqliteAccess();
            string result = obj.ExecuteScalar(sql, null);
            return Convert.ToInt32(result);

        }

        public int GetMaxTaskId() {
            string sql = "select max(task_id) from task";
            SqliteAccess obj = new SqliteAccess();
            string result = obj.ExecuteScalar(sql, null);
            if (result != "null" && result != string.Empty)
            {
                return Convert.ToInt32(result)+1;
            }
            else {
                return 0;
            }
        }
        public int AddTask(string taskName,string keyWord) {
            string sql = @"insert into task (task_id,task_name,keyword,page_index,create_date,page_count,is_deleted)
            values(@task_id,@task_name,@keyword,1,@create_date,0,0) ";
            int taskId = GetMaxTaskId();
            SqliteAccess obj = new SqliteAccess();
            SQLiteParameter[] parameters = new SQLiteParameter[]{
                new SQLiteParameter("task_id",taskId),
                new SQLiteParameter("task_name",taskName),
                new SQLiteParameter("keyword",keyWord),
                new SQLiteParameter("create_date",DateTime.Now)
            };
            obj.ExecuteNonQuery(sql, parameters);
            return taskId;
        }

        public void EditTask(int taskId,string taskName,string keyWord) {
            string sql = "update task set task_name=@task_name,keyword=@keyword where task_id=@task_id";
            SqliteAccess obj = new SqliteAccess();
            SQLiteParameter[] parameters = new SQLiteParameter[]{
                
                new SQLiteParameter("task_name",taskName),
                new SQLiteParameter("keyword",keyWord),
                new SQLiteParameter("task_id",taskId)
            };
            obj.ExecuteNonQuery(sql, parameters);
        }

        public void UpdateTaskInfo(int taskId, int pageIndex, int pageCount)
        {
            string sql = "update task set page_index=@page_index,page_count=@page_count where task_id=@task_id";
            SqliteAccess obj = new SqliteAccess();
            SQLiteParameter[] parameters = new SQLiteParameter[]{
                new SQLiteParameter("task_id",taskId),
                new SQLiteParameter("page_index",pageIndex),
                new SQLiteParameter("page_count",pageCount)
            };
            obj.ExecuteNonQuery(sql, parameters);
        }

        public void DeleteTask(int taskId){
            string sql = "update task set is_deleted =1 where task_id=task_id";
            SqliteAccess obj = new SqliteAccess();
            SQLiteParameter[] parameters = new SQLiteParameter[]{
                new SQLiteParameter("task_id",taskId)
            };
            obj.ExecuteNonQuery(sql, parameters);
        }

    }
}
